package com.feeyo.groundservice.http.utils

import android.app.Application
import android.util.Log
import com.blankj.utilcode.util.Utils
import com.feeyo.groundservice.http.base.BusinessExceptionHandler
import com.feeyo.groundservice.http.beans.BusinessBaseResponse
import com.feeyo.groundservice.http.core.IBaseBean
import com.feeyo.groundservice.http.core.RetrofitUtils
import okhttp3.OkHttpClient
import retrofit2.Retrofit

/**
 *作者：QLH on 2023-09-14
 *描述：网络工具类，仅供内部使用；也可以看看网上开源的RxHttp：https://github.com/liujingxing/rxhttp
 *
 * 使用方法一:
 *    fun getArticle1() {
 *         viewModelScope.launch {
 *             flow {
 *                 val login = NetClientUtil.getRetrofit().create(WannaAndroidApi::class.java)
 *                     .login(hashMapOf<String, Any>().apply {
 *                         put("username", "708937822@qq.com")
 *                         put("password", "caoying520")
 *                     })
 *                 //emit(result)
 *                 delay(3000)
 *                 emit(login)
 *             }.subscribe(object :
 *                 BaseObserver<Any>(msg = "获取数据...", silence = false, composeLoading = {
 *                     loading.value = it
 *                 }) {
 *                 override fun onSuccess(data: Any?) {
 *                     article.value = data.toString()
 *                 }
 *             })
 *         }
 *     }
 *
 *
 * 使用方法二：
 * fun getArticle2() {
 *         viewModelScope.launch {
 *             getData(
 *                 requestCode = {
 *                     val login = NetClientUtil.getRetrofit().create(WannaAndroidApi::class.java)
 *                         .login(hashMapOf<String, Any>().apply {
 *                             put("username", "708937822@qq.com")
 *                             put("password", "caoying520")
 *                         })
 *                     //emit(result)
 *                     delay(3000)
 *                     it.emit(login)
 *                 }, object :
 *                     BaseObserver<Any>(msg = "获取数据...", silence = false, composeLoading = {
 *                         loading.value = it
 *                     }) {
 *                     override fun onSuccess(data: Any?) {
 *                         article.value = data.toString()
 *                     }
 *                 })
 *         }
 *     }
 */
object NetClientUtil {

    /**application中初始化定义baseUrl也可以在使用的时候单独指定
     *
     * @param baseUrl  基础域名
     * @param baseBean 接口单独定制的BaseBean,如果定义全局结构，这里可为null
     * @param allowDataIsNull 是否允许data返回null，默认不允许为空
     * @param businessBaseResponseCls 未被服务器处理过的响应错误：4xx,5xxx ...，一般用不上
     * **/
    fun getRetrofit(
        baseUrl: String? = null,
        baseBean: IBaseBean? = null,
        allowDataIsNull: Boolean? = null,
        businessBaseResponseCls: Class<BusinessBaseResponse>? = null
    ): Retrofit {

        if (baseUrl == null && !RetrofitUtils.isSetBaseUrl()) {
            Log.e(NetClientUtil.javaClass.simpleName, "需要设置基础域名")
            throw Exception("需要设置基础域名")
        }

        return RetrofitUtils.getRetrofitBuilder(
            baseUrl,
            baseBean,
            allowDataIsNull,
            businessBaseResponseCls
        ).build()

    }

    /**初始化**/
    fun init(context: Application): NetClientUtil {
        Utils.init(context)
        return this
    }

    /**设置Http Log开关**/
    fun setLogEnable(logEnable: Boolean): NetClientUtil {
        RetrofitUtils.setLogEnable(logEnable)
        return this
    }

    /**设置BaseUrl**/
    fun setBaseUrl(baseUrl: String): NetClientUtil {
        RetrofitUtils.setBaseUrl(baseUrl)
        return this
    }

    fun setOkhttpClientBuilder(builder: OkHttpClient.Builder): NetClientUtil {
        RetrofitUtils.setOkhttpClientBuilder(builder)
        return this
    }

    /**获取okhttpBuild，在需要扩充配置的时候进行设置**/
    fun getOkhttpClientBuilder(): OkHttpClient.Builder {
        return RetrofitUtils.getOkhttpClientBuilder()
    }

    /**设置全局业务异常处理对象**/
    fun setBusinessExceptionHandler(businessExceptionHandler: BusinessExceptionHandler): NetClientUtil {
        RetrofitUtils.setBusinessExceptionHandler(businessExceptionHandler)
        return this
    }


    /**
     * 定义好字段映射关系
     * 如果能映射到code+msg+data 就去重写本方法，否则就空着
     * 然后就要自己判断业务成功code和对应的处理之类的了
     *
     */
    fun setBaseBean(baseBean: IBaseBean): NetClientUtil {
        RetrofitUtils.setBaseBean(baseBean)
        return this
    }

    /**是否允许data返回null**/
    fun setAllowDataIsNull(allowDataIsNull: Boolean): NetClientUtil {
        RetrofitUtils.setAllowDataIsNull(allowDataIsNull)
        return this
    }

}